package com.china08.yxyapi.api.manage.tec.teaching.lecture.chooseprepare;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lecture.YxyLectureFeedBack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLectureFeedBackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

/**
 * 
 * 挑选备课包--平台备课包--举报
 *
 */
@CrossOrigin
@RestController
public class LectureCrtFeedBackController extends YxyApiBaseController {

    @Autowired
    private YxyLectureFeedBackRepository yxyLectureFeedBackRepository;

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/feedback/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Integer status = 1;
        String prepackId = reqModel.prepackId;
        String reason = reqModel.reason;
        String description = reqModel.description;
        String userNick = getProfile(request, response).getUserNick();
        String areaIdCity = getProfile(request, response).getSchoolAreaIdCity();
        String district = getProfile(request, response).getSchoolAreaIdDistrict();
        String province = getProfile(request, response).getSchoolAreaIdProvince();
        String schoolId = getProfile(request, response).getSchoolId();
        String schoolName = getProfile(request, response).getSchoolName();
        long countByPrepackId = yxyLectureFeedBackRepository.countByPrepackId(prepackId);
        YxyLecturePrePack findById = yxyLecturePrePackRepository.findById(prepackId);
        String countString = countByPrepackId + "";
        YxyLectureFeedBack feedBack = new YxyLectureFeedBack();
        feedBack.setPrepackId(prepackId);
        feedBack.setReason(reason);
        feedBack.setDescription(description);
        feedBack.setUserNick(userNick);
        feedBack.setStatus(status);
        feedBack.setAreaIdCity(areaIdCity);
        feedBack.setAreaIdDistrict(district);
        feedBack.setAreaIdProvince(province);
        // 2017.10.17 Richard.Wang 增加以下两个字段 begin
        feedBack.setPackName(reqModel.packName == null ? "" : reqModel.packName);// 包名
        feedBack.setUploadUserNick(reqModel.uploadUserNick == null ? "" : reqModel.uploadUserNick);// 上传者名称
        // 2017.10.17 Richard.Wang 增加以下两个字段 end
        feedBack.setSchoolId(schoolId);
        feedBack.setSchoolName(schoolName);
        feedBack.setAuditUserName(reqModel.auditUserName);
        feedBack.setPhaseId(reqModel.phaseId);
        feedBack.setPhaseName(reqModel.phaseName);
        feedBack.setSubjectId(reqModel.subjectId);
        feedBack.setSubjectName(reqModel.subjectName);
        feedBack.setVersionId(reqModel.versionId);
        feedBack.setVersionName(reqModel.versionName);
        feedBack.setAreaIdProvince(reqModel.areaIdProvince);
        feedBack.setAreaIdCity(reqModel.areaIdCity);
        feedBack.setAreaIdDistrict(reqModel.areaIdDistrict);
        feedBack.setDownReason(reqModel.downReason);
        feedBack.setIsHighQualityYx(reqModel.isHighQualityYx);
        feedBack.setIsVip(reqModel.isVip);
        feedBack.setFeedBackCount(Integer.valueOf(countString) == null ? 0 : Integer.valueOf(countString) + 1);
        feedBack.setRanks(reqModel.ranks);
        if (findById != null) {
            feedBack.setShare(findById.getShare());
        } else {
            feedBack.setShare(1);
        }
        feedBack.setBookcontentsId(reqModel.bookcontentsId);
        feedBack.setBookcontentsName(reqModel.bookcontentsName);
        feedBack.setIsView(Integer.valueOf(reqModel.isView));
        feedBack.setResCount(findById.getResources().size());
        yxyLectureFeedBackRepository.save(feedBack);
        RespModel model = new RespModel();
        model.setMessage("举报成功");
        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String prepackId;

        public String packName;// 报名

        public String uploadUserNick;// 备课包上传者的名称.

        public String reason;

        public String description;

        public String bookcontentsId;

        public String bookcontentsName;

        public String schoolId;

        public String schoolName;

        public String auditUserName = "";

        public String phaseId;

        public String phaseName;

        public String subjectId;

        public String subjectName;

        public String versionId;

        public String versionName;

        public String areaIdProvince;

        public String areaIdCity;

        public String areaIdDistrict;

        public String status = "0";

        public String downReason = "";

        public Integer isHighQualityYx;

        public Integer isVip;

        public Integer resCount;

        public Integer feedBackCount;

        public Integer ranks;

        public String isView;

        public Integer share;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
